package com.wrupple.muba.base.client.widgets;

import org.cobogw.gwt.user.client.CSS;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.place.shared.PlaceChangeEvent;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.DockLayoutPanel.Direction;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.ResizeComposite;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.wrupple.muba.base.client.module.services.presentation.impl.MubaDesktopManager.HomePlace;
import com.wrupple.muba.common.client.activity.AbstractDesktopModuleController;
import com.wrupple.muba.common.domain.DesktopPlace;
import com.wrupple.muba.common.shared.event.DesktopProcessEvent;
import com.wrupple.muba.widget.client.activity.ActivityPresenter;
import com.wrupple.muba.widget.client.view.Task;
import com.wrupple.muba.widget.client.widgets.panels.PanelWithToolbar;
import com.wrupple.muba.widget.client.widgets.panels.TransitionPanel;
import com.wrupple.muba.widget.client.widgets.toolbar.Toolbar;
import com.wrupple.vegetate.client.event.VegetateEvent;

/**
 * Controlls the behavior and posistion of all widgets in the application
 * 
 * @author japi 
 * 
 */
@Singleton
public class MubaActivityPresenter extends ResizeComposite implements ActivityPresenter {
	
	class PlaceChangeListener implements com.google.gwt.place.shared.PlaceChangeEvent.Handler{
		@Override
		public void onPlaceChange(PlaceChangeEvent event) {
			DesktopPlace place = (DesktopPlace) event.getNewPlace();
			String newActivity = place.getActivity();
			if(HomePlace.HOME_ACTIVITY.equals(newActivity)){
				main.hideToolbar(toolbar);
			}else{
				main.showToolbar(toolbar);
			}
		}
		
	}
	
	@Singleton
	static public class ActivityToolbar extends ResizeComposite implements Toolbar{
		HistoryToolbar history;

		RequestToolbar monitor;
		
		
		LayoutPanel panel;
		@Inject
		public ActivityToolbar(HistoryToolbar history, RequestToolbar monitor) {
			super();
			this.history = history;
			this.monitor = monitor;
			panel = new LayoutPanel();
			panel.add(history);
			panel.add(monitor);
			panel.setWidgetRightWidth(monitor, 0, Unit.PX, 60, Unit.PCT);
			panel.setWidgetLeftWidth(history, 0, Unit.PX, 40, Unit.PCT);
			initWidget(panel);
			CSS.setProperty(panel, "backgroundColor", "gray");
		}

		@Override
		public HandlerRegistration addResizeHandler(ResizeHandler handler) {
			return addHandler(handler, ResizeEvent.getType());
		}

		@Override
		public Direction getDirection() {
			return Direction.NORTH;
		}

		@Override
		public int getSize() {
			return 30;
		}
		
	}

	protected DesktopPlace activeItem;
	
	private PanelWithToolbar main;

	private ActivityToolbar toolbar;
	
	@Inject
	public MubaActivityPresenter(ActivityToolbar toolbar){
		main = new PanelWithToolbar();
		this.toolbar = toolbar;
		initWidget(main);
		AbstractDesktopModuleController.presenter=this;
	}

	@Override
	public AcceptsOneWidget getOutputFeature() {
		TransitionPanel transitionPanel = main.getContentContainer();
		transitionPanel.getElement().setId("DesktopContentArea");
		transitionPanel.setStyleName("application-content-area");
		return transitionPanel;
	}

	@Override
	public Widget getCurrentWidget() {
		return main.getContentContainer().getCurrentWidget();
	}


	@Override
	public void restore(Task<?, ?> task) {
		main.getContentContainer().setWidgetFromFront(task);
	}

	@Override
	public void setEventBus(EventBus bus) {
		bus.addHandler(PlaceChangeEvent.TYPE, new PlaceChangeListener());
		bus.addHandler(VegetateEvent.TYPE, toolbar.monitor);
		bus.addHandler(DesktopProcessEvent.TYPE,toolbar.history);
	}

}